/* 
 *  This file is part of hmvcBlog-blog.
 *
 *  hmvcBlog-blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-blog is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-blog.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.register;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.register.events.in.ShowRegisterViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserNewDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.LoginEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.PasswordEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.in.AddUserEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.AddUserSuccessfulEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.EMailInvalidEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.add.out.LoginExistEvent;
import com.googlecode.hmvc4gwt.framework.client.rpc.ASessionAsyncCallback;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerViewRegistry;

/**
 * 
 * 
 * @author jens.meiss
 */
public class RegisterController extends AControllerViewRegistry<RegisterView> {

	private void buildView() {

		VerticalPanel panel = new VerticalPanel();
		panel.setStylePrimaryName("panelRegister");

		panel.add(getView().getLabelDescription());
		panel.add(getView().getFlexTableFormular());

		getView().setPanelContainer(panel);

	}

	private void emailInvalid() {
		
		Label labelEmailInvalid = new Label(I18N.constants.labelEmailInvalid());
		getView().setLabelEmailInvalid(labelEmailInvalid);
		
		getView().getFlexTableFormular().setWidget(0, 2, labelEmailInvalid);
		
	}

	public void handleEvent(IEvent event) {

		if (event.getClass() == ShowRegisterViewEvent.class) {

			fireEventGlobal(new AddApplicationViewEvent(null, getView()
					.getWidget()));

		} else if (event.getClass() == LoginEmptyEvent.class) {
			
			loginEmpty();
			getView().getButtonRegister().setEnabled(true);
			
		} else if (event.getClass() == LoginExistEvent.class) {
			
			loginExist();
			getView().getButtonRegister().setEnabled(true);
			
		} else if (event.getClass() == PasswordEmptyEvent.class) {
			
			passwordEmpty();
			getView().getButtonRegister().setEnabled(true);
			
		} else if (event.getClass() == EMailInvalidEvent.class) {
			
			emailInvalid();
			getView().getButtonRegister().setEnabled(true);
			
		} else if (event.getClass() == AddUserSuccessfulEvent.class) {
			
			ASessionAsyncCallback.setSessionId(((AddUserSuccessfulEvent) event).getSessionId());
			History.newItem(HistoryToken.dashboard);
			getComponent().terminateComponent();
			
		}

	}

	public void init() {

		initView();
		buildView();

		register(ShowRegisterViewEvent.class);
		register(LoginEmptyEvent.class);
		register(LoginExistEvent.class);
		register(PasswordEmptyEvent.class);
		register(EMailInvalidEvent.class);
		register(AddUserSuccessfulEvent.class);
		
		
	}

	public void initView() {

		Label label = new Label(I18N.constants.labelRegistrationDescription());
		getView().setLabelDescription(label);

		FlexTable flexTable = new FlexTable();

		label = new Label(I18N.constants.labelEmailaddress());
		getView().setLabelEmailaddress(label);
		flexTable.setWidget(0, 0, label);

		TextBox textBox = new TextBox();
		getView().setTextBoxEmail(textBox);
		flexTable.setWidget(0, 1, textBox);

		label = new Label(I18N.constants.labelForename());
		getView().setLabelForename(label);
		flexTable.setWidget(1, 0, label);

		textBox = new TextBox();
		getView().setTextBoxForename(textBox);
		flexTable.setWidget(1, 1, textBox);

		label = new Label(I18N.constants.labelSurename());
		getView().setLabelSurename(label);
		flexTable.setWidget(2, 0, label);

		textBox = new TextBox();
		getView().setTextBoxSurename(textBox);
		flexTable.setWidget(2, 1, textBox);

		label = new Label(I18N.constants.labelLogin());
		getView().setLabelLogin(label);
		flexTable.setWidget(3, 0, label);

		textBox = new TextBox();
		getView().setTextBoxLogin(textBox);
		flexTable.setWidget(3, 1, textBox);

		label = new Label(I18N.constants.labelPassword());
		getView().setLabelPassword(label);
		flexTable.setWidget(4, 0, label);

		PasswordTextBox passwordTextBox = new PasswordTextBox();
		getView().setPasswordTextBox(passwordTextBox);
		flexTable.setWidget(4, 1, passwordTextBox);

		Button buttonRegister = new Button(I18N.constants.buttonRegister());
		buttonRegister.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				registerUser();
			}
		});
		getView().setButtonRegister(buttonRegister);

		Button buttonCancel = new Button(I18N.constants.buttonCancel());
		buttonCancel.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				History.back();
			}
		});
		getView().setButtonCancel(buttonCancel);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(buttonRegister);
		horizontalPanel.add(buttonCancel);

		flexTable.setWidget(5, 1, horizontalPanel);

		getView().setFlexTableFormular(flexTable);
	}

	private void loginEmpty() {
		
		Label labelLoginEmpty = new Label(I18N.constants.labelLoginEmpty());
		getView().setLabelLoginExist(labelLoginEmpty);
		
		getView().getFlexTableFormular().setWidget(3, 2, labelLoginEmpty);
				
	}

	private void loginExist() {
		
		Label labelExistEmpty = new Label(I18N.constants.labelLoginExist());
		getView().setLabelLoginExist(labelExistEmpty);
		
		getView().getFlexTableFormular().setWidget(3, 2, labelExistEmpty);
		
	}

	private void passwordEmpty() {
		
		Label labelPasswordEmpty = new Label(I18N.constants.labelPasswordEmpty());
		getView().setLabelPasswordEmpty(labelPasswordEmpty);
		
		getView().getFlexTableFormular().setWidget(4, 2, labelPasswordEmpty);
		
	}

	private void registerUser() {

		getView().getButtonRegister().setEnabled(false);
		
		if (getView().getLabelEmailInvalid() != null) {
			getView().getFlexTableFormular().remove(getView().getLabelEmailInvalid());
		}
		
		if (getView().getLabelLoginEmpty() != null) {
			getView().getFlexTableFormular().remove(getView().getLabelLoginEmpty());
		}
		
		if (getView().getLabelLoginExist() != null) {
			getView().getFlexTableFormular().remove(getView().getLabelLoginExist());
		}
		
		if (getView().getLabelPasswordEmpty() != null) {
			getView().getFlexTableFormular().remove(getView().getLabelPasswordEmpty());
		}

		UserNewDTO userNewDTO = new UserNewDTO();
		userNewDTO.setEmail(getView().getTextBoxEmail().getText());
		userNewDTO.setForeName(getView().getTextBoxForename().getText());
		userNewDTO.setLogin(getView().getTextBoxLogin().getText());
		userNewDTO.setPassword(getView().getPasswordTextBox().getText());
		userNewDTO.setSureName(getView().getTextBoxSurename().getText());

		fireEventGlobal(new AddUserEvent(userNewDTO, this));

	}

	public void terminate() {
	}
};
